package com.googlecode.continuity.plugin.codecomment.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.googlecode.continuity.plugin.codecomment.client.message.CodeCommentMessages;
import com.googlecode.continuity.plugin.gwtext.client.ConfirmBox;
import com.googlecode.continuity.plugin.svnbrowser.client.SVNRepositoryHandle;
import com.googlecode.continuity.plugin.syntaxhighlight.client.SelectableLinesHighlighter;
import com.googlecode.continuity.plugin.syntaxhighlight.client.UnsupportedLanguageException;

/**
 * CodeCommentScreen
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class CodeCommentScreen extends PopupPanel {
  private CodeCommentArea content;
  private Label goBack;

  private final CodeCommentMessages messages = (CodeCommentMessages) GWT.create(CodeCommentMessages.class);

  public CodeCommentScreen(SVNRepositoryHandle svnHandle, CodePointer file, String code) {
    VerticalPanel frame = new VerticalPanel();
    addStyleName("codeCommentScreen");

    goBack = new HTML(messages.backToBrowser());
    goBack.addClickListener(new ClickListener() {
      public void onClick(Widget widget) {
        hide();
      }
    });
    goBack.addStyleName("goBack");
    goBack.addStyleName("link");
    frame.add(goBack);

    // initialize code comment area
    String name = file.getName();
    String language = name.substring(name.lastIndexOf(".") + 1, name.length());
    SelectableLinesHighlighter highlighter;
    String[] codeLines;
    try {
      // try to find a highlighter for offered language
      highlighter = new SelectableLinesHighlighter(language);
      codeLines = highlighter.highlight(code, true);
    } catch (UnsupportedLanguageException e) {
      codeLines = code.replaceAll("<", "&lt;").split("\n");
      // spans are required for IE7 nowrap
      for (int i = 0; i < codeLines.length; i++) {
        codeLines[i] = "<span>" + codeLines[i] + "</span>";
      }
    }
    file.setLastLine(codeLines.length);
    content = new CodeCommentArea(svnHandle, file);
    content.addLines(codeLines);
    frame.add(content);

    setWidget(frame);
  }

  public void hide() {
    if (content.showingEditedComment()) {
      // TODO: replace with UNDO?
      ConfirmBox box = new ConfirmBox(messages.confirmDiscardComment(),
          new ClickListener() {
            public void onClick(Widget sender) {
              content.hideCommentBoxFinal();
              hideFinal();
            }
          }, messages.yes(), messages.no());
      box.show();
    } else {
      content.hideCommentBoxFinal();
      hideFinal();
    }
  }

  private void hideFinal() {
    super.hide();
  }

  public void show() {
    // fill screen
    setPopupPosition(0, 0);
    setWidth(Window.getClientWidth() + "px");
    setHeight((Window.getClientHeight()) + "px");
    content.setWidth(Window.getClientWidth() + "px");
    super.show();
    content.setHeight((Window.getClientHeight() - goBack.getOffsetHeight()) + "px");
  }
}
